public class MySingleList {
    static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val=val;
        }
    }
    Node head;//当做头节点使用
   //串起节点
    public void cartelink(){
        Node node1=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        head=node1;
    }
    /**
     * 遍历列表
     */
    public void display(){
        Node cur=head;
        while (cur!=null){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }

    /**
     * 查找是否包含关键字key是否在单链表当中
     * @param
     */
    public boolean contains(int key){
        Node cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    /**
     * 得到单链表的长度 O(N)
     * @param //args
     */
    public int size(){
        int size=0;
        Node cur=head;
        while (cur!=null){
          size++;
          cur=cur.next;
        }
        return size;
    }

    /**
     * 头插法
     * @param //args
     */
    public void addFarst(){

    }

    public static void main(String[] args) {
        MySingleList mySingleList=new MySingleList();
        mySingleList.cartelink();//关键点产生链表，使head=node1
        //mySingleList.display();
        //System.out.println(mySingleList.contains(1));
        System.out.println(mySingleList.size());
    }
}


